home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14181 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  50 lines

  1. Path: maestro.inav.net!dkelly
  2. From: dkelly@blue.weeg.uiowa.edu (Dave Kelly)
  3. Newsgroups: comp.lang.c++
  4. Subject: Typecasting a class?  Help!
  5. Date: 28 Mar 96 21:34:58 GMT
  6. Organization: Internet Navigator, Inc.
  7. Message-ID: <dkelly.828048898@maestro.inav.net>
  8. NNTP-Posting-Host: maestro.inav.net
  9.  
  10. OK, we all know we can do this:
  11.  
  12. x = 6;
  13. printf("x is equal to %d\n",x);
  14.  
  15. and it works fine.  But what I want to do is this:
  16.  
  17. class MyInt
  18. {
  19.   public:
  20.     int  iTheValue;
  21.     operator int() { return iTheValue; };
  22.     void SetValue(int iNewValue) { iTheValue = iNewValue; };
  23. };
  24.  
  25. MyInt clInteger;
  26. clInteger.SetValue(6);
  27. printf("clInteger is equal to %d\n", clInteger);
  28.  
  29.  
  30. But it doesn't work unless I do this:
  31.  
  32. printf("clInteger is equal to %d\n", (int)clInteger);
  33.  
  34. Is there any way I can get an instance of MyInt to be seen as
  35. and integer to printf without having to explicitly cast it as above?
  36.  
  37. I'm not sure that this can even be done, but any information will be 
  38. appreciated!
  39.  
  40. Thanks!
  41. -Dave
  42. --
  43. ###### ####### #####  #######      ---------------------------------------
  44. #      #  #  # #   #  # . . #     |   Another tasty e-mail message from   |
  45. ####   #  #  # ###### #     ###   |              Dave Kelly               |
  46. #      #  #  # #    # # `-' # #/  |            dkelly@inav.net            |
  47. ###### #     # ###### ####### ;   |   URL=http://www.inav.net/~dkelly/    |
  48.                             _/     ---------------------------------------
  49.       Electronic Mail     /_/
  50.